<!--
 * @Author: Wangtao
 * @Date: 2020-11-26 20:50:52
 * @LastEditors: Wangtao
 * @LastEditTime: 2021-03-24 17:55:39
-->
<template>
  <div style="height: 100%">
    <callSummary
      :componentHeight="250"
      :randomNum="randomNum"
      :propData="callData"
      moduleType="callCard"
      :showDowload="showDowload"
      :showListen="showListen"
      @changeCustomerRelation="changeCustomerRelation"
      :shadow="true"
    ></callSummary>
  </div>
</template>

<script lang="ts">
import CallSummary from '@/components/commonSummary/index.vue'
import { Component, Vue } from 'vue-property-decorator'
import { CallCardModule } from '@/store/modules/cti/callCard'
import { checkPermission } from '@/utils/directive'
@Component({ name: 'CallSummaryByCallCard', components: { CallSummary } })
export default class CallSummaryByCallCard extends Vue {
  private $bus: any
  private $route: any
  private $router: any
  private callData: any = {}
  private randomNum = 0
  private showDowload: boolean = false
  private showListen: boolean = false

  private mounted () {
    const status = this.$bus.$subscribed('initCustomerCard')
    if (status) {
      return false
    }
    /**
     * importType: 调用的模块|类型 required
     * timeStamp: 时间戳 required
     * phone: 通话号码 required
     * custId: 客户Id required
     * custName: 客户名称 required
     * customerList: 该号码对应的locateCust列表
     * callSheetId: 通话卡片id
     * cdr: 该通话卡片对应的数据
     * cdrName: 来电、外呼
     */
    this.$bus.$on(
      'initCustomerCard',
      (callObj: any) => {
        const routeName = this.$route.params.activeName
        let customerList = []
        let ss: any = []
        let cdr: any = []
        let custId: string = ''
        let custName: string = ''
        if (routeName === 'cdr_call') { // 来电（呼入）
          ss = JSON.parse(window.sessionStorage.getItem('cdr_call') || '[]')
        } else if (routeName === 'cdr_outbound') { // 外呼
          ss = JSON.parse(window.sessionStorage.getItem('cdr_outbound') || '[]')
        }
        for (let i = 0; i < ss.length; i++) {
          if (ss[i].id === callObj.callSheetId) {
            customerList = ss[i].locateCust || []
            custId = ss[i].custId
            custName = ss[i].custName
            cdr = ss[i].body
          }
        }
        this.callData = {
          importType: 'CallCard', // 通话弹屏
          timeStamp: new Date().getTime(),
          phone: callObj.phone,
          custId,
          custName,
          customerList,
          callSheetId: callObj.callSheetId,
          cdr: cdr || { name: '' },
          cdrName: routeName
        }
        this.randomNum = Math.random() + 1
      }
    )
  }

  private changeCustomerRelation (info) {
    // console.log('触发【通话卡片】回调，在此更新客户关联关系:\n', info)
    CallCardModule.updateCardInfo(info)
  }

  private beforeMount () {
    if (checkPermission(null, { value: ['contact_record_audition'] })) { //  试听，下载（通话工作台)
      this.showListen = true
      if (checkPermission(null, { value: ['contact_record_download'] })) {
        this.showDowload = true
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
</style>
